import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/pages/login/index.dart';
import 'package:flutter_massage/pages/main/main_view.dart';
import 'package:flutter_massage/pages/web/web_view.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:get/get.dart';

import '../../service/user_service.dart';

class CommonRoute {
  /// 程序入口
  static Future<Widget> getRootPage() async {
    // Widget page = const SystemSplashPage();
    // bool allowUseApp = Storage().getBool(StorageKey.allowUseApp);
    // if (allowUseApp) {
    //   String key = Storage().getString(StorageKey.userKey);
    //   if (key.empty) {
    //     page = const LoginPage();
    //   } else {
    //     page = const MainPage();
    //   }
    // }
    // return page;

    Widget page = const SystemSplashPage();
    bool allowUseApp = Storage().getBool(StorageKey.allowUseApp);
    if (allowUseApp) {
      // SettingBean bean = await XydUserService.getCacheMineInfoBean(refresh: true);
      // if (bean.prelogin.equals('1')) {
      //   page = const LoginPage();
      // } else {
      page = const MainPage();
      // }
    }
    return page;
  }

  /// 打开登录页面
  static void openLogin() {
    Get.offAll(const LoginPage());
  }

  /// 打开首页
  static void openMain() async {
    Get.offAll(const MainPage());
  }

  /// 打开用户协议
  static void openProtocolPage() => open(WebH5Page(
        url: 'https://lami.chwxkj.top/sites/service-agreement.html',
        title: '用户服务协议',
        needParam: false,
      ));

  /// 打开隐私政策
  static void openPrivacyPage() => open(WebH5Page(
        url: 'https://lami.chwxkj.top/sites/privacy.html',
        title: '隐私协议',
        needParam: false,
      ));

  static void open<T>(
    T page, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.to(
      () => page,
      preventDuplicates: preventDuplicates,
      transition: transition,
      duration: duration,
      fullscreenDialog: fullscreenDialog,
    )?.whenComplete(() => back?.call());
  }

  static void offAll<T>(
    T page, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.offAll(
      () => page,
      transition: transition,
      duration: duration,
      fullscreenDialog: fullscreenDialog,
    )?.whenComplete(() => back?.call());
  }

  static void until(
    String pageName, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.until((route) => route.settings.name == pageName);
  }

  ///关闭路由
  static close() => Get.back();

  static toRoot() => Get.until((route) => route.settings.name == '/');
}
